import { defineStore } from 'pinia'
import { $utils } from '@/utils'
import { useGameStore } from './game'
import { getLotteryTemplate } from '@/api'

export const useXglhcStore = defineStore('xglhc', {
  state: () => ({
    gameTemplateData: [], //游戏渲染数据
    currentGameType: 0, //当前游戏索引
    sonGameTypeIndex: 0, //子游戏类型索引
    gameStore: useGameStore(),
    
  }),
  getters: {
    getGameComponentTag() {
      return this.getPlayTypeCodeList[this.currentGameType]
    },

    // 六合彩所有游戏玩法
    getPlayTypeCodeList() {
      if ($utils.isEmpty(this.gameTemplateData)) return []

      const list = this.gameTemplateData.map((game) => {
        const name = game.playTypeCode + '_' + 'GAME'
        return name
      })
      return list
    },

    // 获得 选中游戏类型数据
    getCurrentGameData() {
      if ($utils.isEmpty(this.gameTemplateData)) return []
      return this.gameTemplateData[this.currentGameType]
    },

    // 选中子游戏的类型
    getSonGameType() {
      //有子类型就找子类型的 类型
      return this.getCurrentGameData.playTypeCode
    },
  },

  actions: {
    // 入口，初始化
    async init() {
      if ($utils.isEmpty(this.gameTemplateData)) {
        this.fetchGameTemplateData()
      } else {
        this.gameStore.setCurrentSonGame(this.getSonGameType)
      }
    },

    // 当前游戏索引
    setCurrentGameType(index) {
      this.currentGameType = index
      this.gameStore.setCurrentSonGame(this.getSonGameType)
    },

    // 子游戏类型索引
    setSonGameTypeIndex(index) {
      this.sonGameTypeIndex = index
      this.gameStore.setCurrentSonGame(this.getSonGameType)
    },

    // 立即下注
    betNow(refs) {
      refs['gameRef'].betNow()
    },

    // 清空所有下注内容
    clearInvestList(refs) {
      refs['gameRef'].updateInvestList('clear')
    },

    // 删除一个游戏的下注订单
    deleteAnInvest(data) {
      data.dom['gameRef'].updateInvestList('del', data.data)
    },

    // 撤消订单
    withdrawAnOrder() {},

    // 获得游戏渲染数据
    async fetchGameTemplateData() {
      try {
        const { data } = await getLotteryTemplate({ 
          lotteryCode: this.gameStore.currentGame,
          handicapCode: this.gameStore.currentHandicapCode
        })

        data
          .find((item) => item.playTypeName === '连码')
          ?.lotteryPlayTypeList.find((lmItem) => lmItem.playTypeName === '三中二')
          ?.cachePlayList?.forEach((cacheItem) => {
            // 连码  三中二 maxOdds 用 odds
            cacheItem.maxOdds = cacheItem.odds
          })

        this.gameTemplateData = data
        this.gameStore.setCurrentSonGame(this.getSonGameType)
      } catch (e) {
        console.log(e)
      }
    },
  },
})
